using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using BoOnce.Components;

namespace BoOnce.GameLogic.Entities.Drawable.Scenery
{
    class Bus : PhysicalScenery
    {
        #region Attributes
        public float Speed = -0.01f;

        private Texture2D _BusTexture;

        #region Attributes for vibration management
        private float _Scale = 1.0f;
        private double _DurationBetweenStep = 1500;
        private float _MinScale = 0.96f;
        private float _MaxScale = 1.04f;
        private bool _StepUp = true;
        private double _TimeStepBeginning = -1;
        private Random _Random;
        #endregion
        #endregion

        #region Constructor
        public Bus(Texture2D texture, Vector2 position, float mass)
            : base(position, texture.Width, texture.Height, mass)
        {
            _BusTexture = texture;
            IsStatic = false;
            _Random = new Random();
        }
        #endregion

        #region DrawableEntity
        public override void QuickDraw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(_BusTexture, Camera.ToScreenPosition(Position), null, Color.White, _Body.TotalRotation, new Vector2(Width / 2.0f, Height / 2.0f), _Scale, SpriteEffects.None, 0.0f); ;
        }

        public override void Update(GameTime gameTime)
        {
            Position += new Vector2(0, Speed*(float)gameTime.ElapsedGameTime.TotalMilliseconds);

            // manage bus vibration
            double currentTime = gameTime.TotalGameTime.TotalMilliseconds; 
            if (_TimeStepBeginning == -1) _TimeStepBeginning = currentTime;
            if (_TimeStepBeginning + _DurationBetweenStep < currentTime)
            {
                _TimeStepBeginning = currentTime;
                _StepUp = !_StepUp;
            }
            
            //Collision += _Geom.Collision(_Geom,_Geom,
               //_Geom.Intersect(
            if (_StepUp)
            {
                _Scale = MathHelper.Lerp(_MinScale, _MaxScale, (float)((currentTime - _TimeStepBeginning) / _DurationBetweenStep));
            }
            else
            {
                _Scale = MathHelper.Lerp(_MaxScale, _MinScale, (float)((currentTime - _TimeStepBeginning) / _DurationBetweenStep));
            }

            float rnd = (((float)_Random.NextDouble()) - 0.5f)*2.0f; // rnd = [-1f,1f]
            Position += new Vector2(0.8f * rnd, 0.2f * rnd);
        }
        #endregion
    }
}
